package com.boc.langxiang.presenter.me;

import com.boc.langxiang.contract.me.OrdersWaitReceiveFragmentContract;
import com.boc.langxiang.entity.request.ConfirmReceiptRequest;
import com.boc.langxiang.entity.request.OrdersWaitReceiveRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrdersWaitReceiveContent;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/24.
 */
public class OrdersWaitReceiveFragmentPresenter implements OrdersWaitReceiveFragmentContract.Presenter {

    private final ApiService mService;
    private OrdersWaitReceiveFragmentContract.View mView;


    public OrdersWaitReceiveFragmentPresenter(OrdersWaitReceiveFragmentContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }

    @Override
    public void toReceiptOrders(String page) {
        OrdersWaitReceiveRequest request = new OrdersWaitReceiveRequest();
        request.page = page;
        Observable<BaseResponse<OrdersWaitReceiveContent>> toReceiptOrders = mService.toReceiptOrders(request.params());
        toReceiptOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersWaitReceiveContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersWaitReceiveContent> response) {
                        OrdersWaitReceiveContent content = response.getContent();
                        if (content == null) {
                            mView.toReceiptOrders(null);
                        } else {
                            List<ProductListEntity> list = content.getList();
                            mView.toReceiptOrders(list);
                        }
                    }
                });
    }

    @Override
    public void confirmReceipt(String id) {
        mView.showLoading();
        ConfirmReceiptRequest request = new ConfirmReceiptRequest();
        request.id = id;
        Observable<BaseResponse> confirmReceipt = mService.confirmReceipt(request.params());
        confirmReceipt.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.conFirmReceipt(response);
                    }
                });
    }
}
